package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.ExternalLink;
import com.sc.nft.entity.dto.ExternalLinkPageAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkUpdateAdminDTO;
import com.sc.nft.entity.dto.ExternalLinkWebDTO;
import com.sc.nft.entity.vo.AdminExternalLinkListVO;
import com.sc.nft.mapper.ExternalLinkMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * 外链实体表(sc_external_link)数据DAO
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-03-03 17:10:48
 */
@Slf4j
@Repository
public class ExternalLinkDao extends ServiceImpl<ExternalLinkMapper, ExternalLink> {

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }

    /**
     * 后端获取集合
     *
     * @param externalLinkPageAdminDTO
     * @return
     */
    public Page<AdminExternalLinkListVO> pageAdmin(ExternalLinkPageAdminDTO externalLinkPageAdminDTO) {
        return baseMapper.pageAdmin(externalLinkPageAdminDTO.toMp(), externalLinkPageAdminDTO);
    }

    /**
     * 根据唯一标识ID修改数据
     *
     * @param externalLinkUpdateAdminDTO
     * @return
     */
    public Boolean updateByIdAndIconId(ExternalLinkUpdateAdminDTO externalLinkUpdateAdminDTO) {
        return SqlHelper.retBool(baseMapper.updateByIdAndIconId(externalLinkUpdateAdminDTO));
    }

    /**
     * 前端根据dao获取所有数据列表
     *
     * @param daoId
     * @return
     */
    public Page<AdminExternalLinkListVO> webPage(ExternalLinkWebDTO externalLinkWebDTO) {
        return baseMapper.webPage(externalLinkWebDTO.toMp(),externalLinkWebDTO);
    }

    public Boolean selectByDaoIdUrlAndIconIdExist(Long iconId, String url, Long daoId) {
        QueryWrapper<ExternalLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url);
        queryWrapper.eq("icon_id", iconId);
        queryWrapper.eq("dao_id", daoId);
        return SqlHelper.retBool(baseMapper.selectCount(queryWrapper));
    }

    public Boolean selectByDaoIdUrlAndIconIdExistNotId(Long iconId, String url, Long daoId,Long id) {
        QueryWrapper<ExternalLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url", url);
        queryWrapper.eq("icon_id", iconId);
        queryWrapper.eq("dao_id", daoId);
        queryWrapper.ne("id", id);
        return SqlHelper.retBool(baseMapper.selectCount(queryWrapper));
    }

}